/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.retail;

import static com.opensymphony.xwork2.Action.ERROR;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionSupport;
import database.ProductDAO;
import java.util.List;
import model.Product;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Thai Ha Cong
 */
@Namespace("/")
public class SimpleSearchProductAction extends ActionSupport {
    
    private String title;
    private String description;
    private String keyword;
    private List<Product> productsResultList;
    private ProductDAO productDAO = new ProductDAO();
    
    public SimpleSearchProductAction() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public List<Product> getProductsResultList() {
        return productsResultList;
    }

    public void setProductsResultList(List<Product> productsResultList) {
        this.productsResultList = productsResultList;
    }
    
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
    
    @Action(value = "searchProduct", results = {
        @Result(name = "success", location = "/home.jsp"),
        @Result(name = "error", location = "/redirect/searchRdr.jsp")
    })
    public String searchProduct() {
        if (keyword == null || keyword.length() < 3) {
            return ERROR;
        } else {
            productsResultList = productDAO.searchForProductsByKeyword(keyword);

            title = "Search Results for keyword '" + keyword + "'";
            description = "Search Product Results";

            return SUCCESS;
        }
    }
}